package example;

// enum status//棋子颜色枚举类
// {
//     BLACK, WHITE,EMPTY
// }
public class Border {
    private int[][] chessboard;
    private int size = 15;

    public Border() {
        chessboard = new int[size][size];
        for(int i = 0; i < size; i++) {
            for(int j = 0; j < size; j++) {
                chessboard[i][j] = 0;
            }
        }
    }
    public int getSize()
    {
        return size;
    }
    public void setSize(int size)
    {
        this.size = size;
    }

    public int[][] getChessboard()
    {
        return chessboard;
    }
    public void setChessboard(int[][] chessboard)
    {
        this.chessboard = chessboard;
    }

    //检查落子是否合法
    public boolean checkMove(int x, int y)
    {

        if(this.chessboard[x][y]== 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //落子
    public void move(int x, int y, Player player)
    {
        //获取鼠标坐标x，y
        if(checkMove(x, y))
        {
            this.chessboard[x][y] = player.getColor();
        }
    }

    // 撤销落子
    public void undoMove(int x, int y) {
        this.chessboard[x][y] = 0;
    }

    // 判断游戏是否结束
    public boolean isGameOver() {
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                if (chessboard[x][y] != 0) {
                    Player player = new Player(chessboard[x][y]);
                    if (isWin(x, y, player)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public boolean isWin(int x, int y, Player player) {
        int color = player.getColor();
        int[][] directions = {
                {1, 0},   // 水平
                {0, 1},   // 垂直
                {1, 1},   // 对角线 /
                {1, -1}   // 对角线 \
        };

        for (int[] dir : directions) {
            int count = 1; // 当前位置已经有一个棋子

            // 正向检查
            for (int i = 1; i < 5; i++) {
                int newX = x + dir[0] * i;
                int newY = y + dir[1] * i;

                if (newX >= 0 && newX < size && newY >= 0 && newY < size &&
                        chessboard[newX][newY] == color) {
                    count++;
                } else {
                    break;
                }
            }

            // 反向检查
            for (int i = 1; i < 5; i++) {
                int newX = x - dir[0] * i;
                int newY = y - dir[1] * i;

                if (newX >= 0 && newX < size && newY >= 0 && newY < size &&
                        chessboard[newX][newY] == color) {
                    count++;
                } else {
                    break;
                }
            }

            if (count >= 5) {
                return true;
            }
        }

        return false;
    }
}
